/**
 * @author helio
 */
if(!SALE.dao) SALE.dao = {}
if(!SALE.dao.local) SALE.dao.local = {}
if(!SALE.dao.local.PersonnelDao) SALE.dao.local.PersonnelDao = function(){
	var userDoc;
}

SALE.dao.local.PersonnelDao.prototype = {	
	
		/**
		 * 
		 */
		initUserDoc : function() {
			if(!this.userDoc) {
				this.userDoc = LocalXMLFileUtil.getUserDoc();
			}
			
			return this.userDoc;
		},
		
		saveUserDoc : function() {
			LocalXMLFileUtil.saveXML(this.userDoc,LocalXMLFileUtil.getUserFile());
		},
		
		insertUserInfo : function(userJson) {
			var root = this.userDoc.documentElement;
			var maxid = root.getAttribute("maxid");
			if(!maxid) {
				maxid = 0;
			}
			var id = parseInt(maxid) + 1;
			userJson["id"] = id;
			root.setAttribute("maxid",id);
			var userNode = this.userDoc.createElement("user");
			
			this.updatePersonnel(userJson, userNode);			
			root.appendChild(userNode);
			
			this.saveUserDoc();
		},
		
		modifyUserInfo : function(userJson) {
			var userNode = this.searchUserByID(userJson.id);
			
			this.updatePersonnel(userJson, userNode);
			
			this.saveUserDoc();
		},
		
		deleteUserInfo : function(id) {
			var userNode = this.searchUserByID(id);
			var root = this.userDoc.documentElement;
			root.removeChild(userNode);
		},
				
		updatePersonnel : function(userJosn, userNode) {
			
			for(var attri in userJosn) {	
				
				if(attri.indexOf("old") != 0 && userJosn[attri] && userJosn[attri] != "") {
					var attriNodes = Ext.DomQuery.select("/" + attri, userNode);
					if(attriNodes != null && attriNodes.length > 0) {
						userNode.removeChild(attriNodes[0]);
					}
					
					var attriNode = this.userDoc.createElement(attri);
					var contextNode = this.userDoc.createCDATASection(userJosn[attri]);
					attriNode.appendChild(contextNode);
					userNode.appendChild(attriNode);
				}
			}
		},
		
		searchUserByID : function(id) {
			var root = this.userDoc.documentElement;
			var objArray = Ext.query("/user:has(id:nodeValue(" + id+ "))", root);
			var result;
			if(objArray.length == 1) {
				result = objArray[0];
			}
			
			return result;
		},
		
		searchUserLoginJson : function(code, password) {
			if(!this.userDoc) {
				this.initUserDoc();
			}
			var root = this.userDoc.documentElement;
			var objArray = Ext.query("/user:has(loginid:nodeValue(" + code+ "))", root);
			var result = {success:false};
			
			if(objArray.length < 1) {
				result["message"] = "此用户名还没有注册。";
			}
			
			Ext.each(objArray, function(obj) {				
				var passwordArr = Ext.DomQuery.select("/password/*", obj);
				if(password && passwordArr[0].nodeValue === password) {
					var children = obj.childNodes;
					for (var i = 0; i < children.length; i++) {
						var child = children[i];					
						if (child.nodeType == 1) {
							if (!result[child.nodeName] && child.childNodes[0] != null) {
								result[child.nodeName] = child.childNodes[0].nodeValue;
							}
						}
					}
					
					result.success = true;
				} else {
					result["message"] = "密码错误。";
				}
		    });
						
			return result;
		},
		
		searchUserJsonByID : function(id) {
			
			var resultNode = this.searchUserByID(id);
			
			var result = {};
			
			if (resultNode != null) {
				var children = resultNode.childNodes;
				for (var i = 0; i < children.length; i++) {
					var child = children[i];					
					if (child.nodeType == 1) {
						if (!result[child.nodeName] && child.childNodes[0] != null) {
							result[child.nodeName] = child.childNodes[0].nodeValue;
						}
					}
				}
			}
			
			return result;
		},
		
		buildUsersJson : function() {
			var root = this.initUserDoc().documentElement;
			if(root != null) {
				var objArray = Ext.query("/user", root);
				var usersJson = {users: []};
				
				Ext.each(objArray, function(user) {
					var children = user.childNodes;
					var userJson = {};
					for(var i = 0; i < children.length; i++) {
						var child = children[i];
						
						if(child.nodeType == 1) {
							if (!userJson[child.nodeName] && child.childNodes[0] != null) {
								userJson[child.nodeName] = child.childNodes[0].nodeValue;
							}
						}			
					}
					
					usersJson.users.push(userJson);
				});
				
				return usersJson;
			}
		}
}